﻿using Microsoft.AspNetCore.Mvc.Rendering;
using System.Collections.Generic;
using System.Linq;

namespace Cxuu.Models
{
    public static partial class AdminUserDeptExtensions
    {


        /// <summary>
        /// 递归遍历类别列表并将其排序
        /// </summary>
        /// <param name="article_Cates"></param>
        /// <param name="treeModels"></param>
        /// <param name="parentId"></param>
        /// <param name="depth"></param>
        public static void IndentedCate(IEnumerable<AdminUserDept> article_Cates, IList<ViewAdminUserDept> treeModels, int parentId = 0, int depth = 0)
        {
            depth++;
            List<AdminUserDept> cates = article_Cates.Where(a => a.Pid == parentId).ToList();
            foreach (AdminUserDept item in cates)
            {
                ViewAdminUserDept treeModel = new()
                {
                    Id = item.Id,
                    Title = item.DeptName,
                    DeptName = item.DeptName,
                    OrderNum = item.OrderNum,
                    Remark = item.Remark,
                    Depth = depth,
                    Spread = "true",
                };
                treeModels.Add(treeModel);
                IndentedCate(article_Cates, treeModel.Children, treeModel.Id, depth);
            }
        }
        /// <summary>
        /// 转换类别列表模型
        /// </summary>
        /// <param name="article_Cates"></param>
        /// <returns></returns>
        public static IEnumerable<ViewAdminUserDept> AsViewArticleCates(this IEnumerable<AdminUserDept> article_Cates)
        {
            List<ViewAdminUserDept> treeModels = new();
            IndentedCate(article_Cates, treeModels);
            
            List<ViewAdminUserDept> outModels = new();
            foreach (var item in treeModels)
            {
                MegCate(outModels, item);
            }
            return outModels;
        }

        static void MegCate(IList<ViewAdminUserDept> outModels, ViewAdminUserDept cate)
        {
            outModels.Add(cate);
            foreach (var item in cate.Children)
            {
                item.ChildrenCount += item.Children.Count;
                MegCate(outModels, item);
            }
        }

        /// <summary>
        /// selectlist 引用
        /// </summary>
        /// <param name="article_Cates"></param>
        /// <returns></returns>
        public static IEnumerable<SelectListItem> AsSelectListItems(this List<AdminUserDept> article_Cates)
        {
            List<SelectListItem> valueCategorys = new();
            IEnumerable<ViewAdminUserDept> categories = article_Cates.AsViewArticleCates();
            for (int i = 0; i < categories.Count(); i++)
            {
                var folder = categories.ElementAt(i);
                var headString = "";
                if (folder.Depth > 1)
                {
                    if (folder.ChildrenCount > 0)
                        headString += "├";
                    else
                        headString += "└";
                    for (int j = 2; j < folder.Depth; j++)
                        headString += "─ ";
                }
                var selecttext = string.Format("{0}{1}", headString, folder.DeptName);

                valueCategorys.Add(new SelectListItem { Text = selecttext, Value = folder.Id.ToString(), Disabled = false });
            }
            return valueCategorys;
        }

        /// <summary>
        /// 类别列表 引用
        /// </summary>
        /// <param name="article_Cates"></param>
        /// <returns></returns>
        public static IEnumerable<ViewAdminUserDept> AsCateListItems(this List<AdminUserDept> article_Cates)
        {
            List<ViewAdminUserDept> cate = new();
            IEnumerable<ViewAdminUserDept> categories = article_Cates.AsViewArticleCates();
            for (int i = 0; i < categories.Count(); i++)
            {
                ViewAdminUserDept folder = categories.ElementAt(i);
                string headString = "";
                if (folder.Depth > 1)
                {
                    if (folder.ChildrenCount > 0)
                        headString += "├ ";
                    else
                        headString += "└";
                    for (int j = 2; j < folder.Depth; j++)
                        headString += "─";
                }
                string selecttext = string.Format("{0}{1}", headString, folder.DeptName);
                
                cate.Add(new ViewAdminUserDept
                { 
                    Id = folder.Id, 
                    Pid = folder.Pid,
                    DeptName = selecttext,
                    OrderNum = folder.OrderNum,
                    Remark = folder.Remark,
                    GreatTime = folder.GreatTime,
                });
            }
            return cate.ToList();
        }

    }
}
